package com.shop2cn.iapi.sqdistributor.testcase;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.sqdistributor.parameter.request.AuditDistributorApplyReq;
import com.shop2cn.iapi.sqdistributor.parameter.request.DistributorApplyReq;
import com.shop2cn.iapi.sqdistributor.service.AuditDistributorApplyCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.shenqi.SqDistributorService;
import com.ymttest.common.order.userquery.util.Utils;
import com.ymttest.database.model.sqdistributor.DistributorApply;
import com.ymttest.database.model.sqdistributor.DistributorRelation;
import com.ymttest.database.sqlwapper.sqdistributor.DistributorApplyWrapper;
import com.ymttest.database.sqlwapper.sqdistributor.DistributorRelationWrapper;
import com.ymttest.utils.TestDataManager;

public class Ts_AuditDistributorApply {
	
	//java接口
	private static AuditDistributorApplyReq auditDistributorApplyReq;
	private static AuditDistributorApplyCall auditDistributorApplyCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("审核分销员申请");
	}

	@Before
	public void caseUp() {
		auditDistributorApplyReq = new AuditDistributorApplyReq();
		auditDistributorApplyCall = new AuditDistributorApplyCall();	
	}
	
	@AfterClass
	public static void tearDown() {
		Logger.generateResult(auditDistributorApplyCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	public void verifyDb() throws Exception {
		
		Logger.comment("验证distributor_apply表");
		
		DistributorApplyWrapper distributorApplyWrapper = new DistributorApplyWrapper();
		DistributorRelationWrapper distributorRelationWrapper = new DistributorRelationWrapper();

		DistributorApply actualResult = distributorApplyWrapper
				.selectByPrimaryKey(auditDistributorApplyReq.getId());
		DistributorRelation distributorRelation = distributorRelationWrapper
				.selectByMerchantIdAndDistributorId(
						actualResult.getMerchant_id(),
						actualResult.getApply_user_id());

		Logger.verifyEquals(auditDistributorApplyReq.getAuditorId(),
				actualResult.getAuditor_id(), "验证Auditor_id");
		Logger.verifyEquals(
				Utils.falseIfNull(auditDistributorApplyReq.getPass()),
				actualResult.getAudit_status() == 1 ? true : false,
				"验证Audit_status");
		Logger.verifyNotNull(actualResult.getAudit_time(), "验证Audit_time");
		Logger.verifyEquals(
				auditDistributorApplyReq.getRemark() == null ? actualResult
						.getAudit_remark() : auditDistributorApplyReq
						.getRemark(), actualResult.getAudit_remark(),
				"验证Audit_remark");
		
		if (actualResult.getAudit_status() == 1) {
			
			verifyDistributorRelationTable(distributorRelation, actualResult);
		} else {
			
			Logger.verifyEquals(null, distributorRelation, "审核未通过，distributor_relation表没有数据");
		}
	}
	
	public void verifyDistributorRelationTable(
			DistributorRelation distributorRelation,
			DistributorApply distributorApply) throws Exception {

		Logger.comment("验证distributor_relation表");
		
		Logger.verifyEquals(distributorApply.getMerchant_id(),
				distributorRelation.getMerchant_id(), "验证Merchant_id");
		Logger.verifyEquals(
				distributorApply.getApply_user_id(),
				distributorRelation.getDistributor_id(), "验证Distributor_id");
		Logger.verifyEquals(distributorApply.getApply_user_name(),
				distributorRelation.getDistributor_name(), "验证Distributor_name");
		Logger.verifyEquals(distributorApply.getApply_user_phone(),
				distributorRelation.getDistributor_phone(), "验证Distributor_phone");
		Logger.verifyEquals(
				distributorApply.getReferrer_id(),
				distributorRelation.getParent_distributor_id(),
				"验证Parent_distributor_id");
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_AuditDistributorApply_001() {
		Logger.start(true, "分销员信息审核为true，成功");
		
		try {
			
			Long id = SqDistributorService.getDistributorApplyId(DistributorApplyReq.createStandardDistributorApply(502139100L));
			
			auditDistributorApplyReq.setAuditorId((long)TestDataManager.getRandomIntNum(5));
			auditDistributorApplyReq.setId(id);
			auditDistributorApplyReq.setPass(true);
			auditDistributorApplyReq.setRemark(TestDataManager.randomStr(5));
			
			auditDistributorApplyCall.setData(auditDistributorApplyReq);
			auditDistributorApplyCall.callService();
			
			Logger.verifyEquals(true, auditDistributorApplyCall.getSuccess(), "验证success");
			
			verifyDb();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_AuditDistributorApply_002() {
		Logger.start(true, "分销员信息审核为false，成功");
		
		try {
			
			auditDistributorApplyReq.setAuditorId((long)TestDataManager.getRandomIntNum(5));
			auditDistributorApplyReq.setId(1L);
			auditDistributorApplyReq.setPass(false);
			auditDistributorApplyReq.setRemark(TestDataManager.randomStr(5));
			
			auditDistributorApplyCall.setData(auditDistributorApplyReq);
			auditDistributorApplyCall.callService();
			
			Logger.verifyEquals(true, auditDistributorApplyCall.getSuccess(), "验证success");
			
			verifyDb();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_AuditDistributorApply_003() {
		Logger.start(true, "AuditorId为null，审核失败");
		
		try {
			
			//auditDistributorApplyReq.setAuditorId((long)TestDataManager.getRandomIntNum(5));
			auditDistributorApplyReq.setId(1L);
			auditDistributorApplyReq.setPass(false);
			auditDistributorApplyReq.setRemark(TestDataManager.randomStr(5));
			
			auditDistributorApplyCall.setData(auditDistributorApplyReq);
			auditDistributorApplyCall.callService();
			
			Logger.verifyEquals(false, auditDistributorApplyCall.getSuccess(), "验证success");
			Logger.verifyEquals("auditorId:must be greater than or equal to 1",
					auditDistributorApplyCall.getMsg(), "验证success");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_AuditDistributorApply_004() {
		Logger.start(true, "id为null，审核失败");
		
		try {
			
			auditDistributorApplyReq.setAuditorId((long)TestDataManager.getRandomIntNum(5));
			//auditDistributorApplyReq.setId(1L);
			auditDistributorApplyReq.setPass(false);
			auditDistributorApplyReq.setRemark(TestDataManager.randomStr(5));
			
			auditDistributorApplyCall.setData(auditDistributorApplyReq);
			auditDistributorApplyCall.callService();
			
			Logger.verifyEquals(false, auditDistributorApplyCall.getSuccess(), "验证success");
			Logger.verifyEquals("id:must be greater than or equal to 1",
					auditDistributorApplyCall.getMsg(), "验证success");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_AuditDistributorApply_005() {
		Logger.start(true, "pass为null，审核成功");
		
		try {
			
			auditDistributorApplyReq.setAuditorId((long)TestDataManager.getRandomIntNum(5));
			auditDistributorApplyReq.setId(1L);
			//auditDistributorApplyReq.setPass(false);
			auditDistributorApplyReq.setRemark(TestDataManager.randomStr(5));
			
			auditDistributorApplyCall.setData(auditDistributorApplyReq);
			auditDistributorApplyCall.callService();
			
			Logger.verifyEquals(true, auditDistributorApplyCall.getSuccess(), "验证success");
			verifyDb();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
